package com.qvtu.web.controller;


import com.qvtu.web.config.AjaxResult;
import com.qvtu.web.domain.Users;
import com.qvtu.web.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyy
 * @since 2024-06-14
 */
@Controller
@RequestMapping("/userManage")
public class UsersController {
    @Autowired
    private IUsersService usersService;


    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        usersService.removeById(id);
        return AjaxResult.success();
    }
    @RequestMapping("/edit{id}")
    public String booksEdit(Model model, @PathVariable("id") Integer num){
        Users usersList =usersService.getById(num);
        model.addAttribute("usersList",usersList);
        return "MainInterface/userEdit";
    }


    @RequestMapping("/saveOrUpdata")
    @ResponseBody
    public AjaxResult update(Users users){
        if(users.getId() == null){
            users.setCreateTime(LocalDateTime.now());
            users.setUpdataTime(LocalDateTime.now());
        }else{
            users.setUpdataTime(LocalDateTime.now());
        }
        usersService.saveOrUpdate(users);
        return AjaxResult.success();
    }

}

